//声明构造函数
function Promise(executor) {
    //状态属性
    this.PromiseState = 'pending';
    //结果值
    this.PromiseResult = undefined;
    //声明属性 保存待执行的回调
    this.callbacks = [];// 可能会保存多组回调

    //声明 success
    let success = (value) => {
        //判断
        if (this.PromiseState !== 'pending') return;
        //修改状态
        this.PromiseState = 'fulfilled';
        //设置结果值
        this.PromiseResult = value;
        setTimeout(() => {
            //执行 待执行的回调
            if (this.callbacks.length > 0) {
                this.callbacks.forEach(cbObj => {
                    //执行成功的回调
                    cbObj.ok(this.PromiseResult);
                })
            }
        })
    }

    //声明 fail
    let fail = (reason) => {
        //判断
        if (this.PromiseState !== 'pending') return;
        //修改状态
        this.PromiseState = 'rejected';
        //设置结果值
        this.PromiseResult = reason;
        
        setTimeout(() => {
            //执行待执行的回调
            if (this.callbacks.length > 0) {
                this.callbacks.forEach(cbObj => {
                    //执行成功的回调
                    cbObj.ng(this.PromiseResult);
                })
            }
        })
    }

    try {
        //调用执行器函数
        executor(success, fail);
    } catch (e) {
        //修改状态和结果值
        fail(e);
    }
}


//原型声明 then 方法    on 当..时候      
Promise.prototype.then = function (onFulfilled, onRejected) {
    //调整 onRejected 的值
    if (typeof onRejected !== 'function') {
        onRejected = reason => { throw reason }
    }

    //调整 onFulFilled 的值
    if (typeof onFulfilled !== 'function') {
        onFulfilled = value => value;
    }

    return new Promise((resolve, reject) => {
        //声明函数, 处理结果 改变 promise 的状态
        let callback = (type) => {
            try {
                //执行成功的回调
                let res = type(this.PromiseResult);
                //判断是否为一个 promise 对象
                if (res instanceof Promise) {
                    //是 promise 的情况
                    res.then(v => {
                        resolve(v);
                    }, r => {
                        reject(r);
                    })
                } else {
                    //不是 promise 的情况
                    resolve(res);
                }
            } catch (e) {
                reject(e);
            }
        }

        //执行回调
        //判断 promise 对象的状态
        if (this.PromiseState === 'fulfilled') {
            setTimeout(() => {
                callback(onFulfilled);
            })
        }

        if (this.PromiseState === 'rejected') {
            setTimeout(() => {
                callback(onRejected);
            })
        }

        //针对 pending 做处理
        if (this.PromiseState === 'pending') {
            //  onFulfilled     onRejected
            //保存两个回调函数
            this.callbacks.push({
                ok: function () {
                    callback(onFulfilled);
                },
                ng: function () {
                    callback(onRejected);
                }
            })
        }
    })
}

//声明 catch
Promise.prototype.catch = function (onRejected) {
    return this.then(undefined, onRejected);
}

//声明 Promise.resolve
Promise.resolve = function(value){
    //返回结果
    return new Promise((resolve, reject) => {
        if(value instanceof Promise){
            //简便操作
            value.then(resolve, reject);
        }else{
            resolve(value);
        }
    })
}

//声明 Promise.reject
Promise.reject = function(reason){
    return new Promise((resolve, reject) => {
        reject(reason);
    })
}